﻿using CommunergyLogic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Linq;
using System.Collections.Generic;
using System;
using Communergy.Common;
using Communergy.UnitTesting;
using Communergy.Repository;
using Communergy.Repository.EntityFramework;

namespace CommunergyLogicTests
{
    
    
    /// <summary>
    ///This is a test class for AutomaticDataHelperTest and is intended
    ///to contain all AutomaticDataHelperTest Unit Tests
    ///</summary>
    [TestClass()]
    public class AutomaticDataHelperTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for Insert_ElectricityRealtime
        ///</summary>
        [TestMethod()]
        public void Insert_ElectricityRealtimeTest()
        {
            communergyEntities db = new communergyEntities();
            var meterGuid = Guid.NewGuid();

            var newMeter = new ElectricityMeter
                            {
                                me_meterGuid = meterGuid,
                                me_meterName = UnitTestHelper.RandomString(15)
                            };
            db.AddToElectricityMeter(newMeter);
            db.SaveChanges();

            var initialMeterReadings = newMeter.ElectricityRealtime.Count;
            Assert.AreEqual(0, initialMeterReadings);

            
            int recordsToCreate = UnitTestHelper.RandomInt(100,1000);
            var newRealtimeEnergyReadings = CommunergyTestHelper.CreateRealtimeRecords(recordsToCreate, 6, DateTime.Now);
            
            AutomaticDataHelper.Insert_ElectricityRealtime(meterGuid.ToString(), newRealtimeEnergyReadings);

            
            communergyEntities readDb = new communergyEntities();
            var readMeter = readDb.ElectricityMeter.Include("ElectricityRealtime").Where(m => m.me_meterGuid == meterGuid).FirstOrDefault();
            Assert.IsNotNull(readMeter);
            Assert.AreEqual(recordsToCreate, readMeter.ElectricityRealtime.Count);

            var totalDbJoules = readMeter.ElectricityRealtime.Sum(er => er.cc_joules);
           


        }

        
    }
}
